<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
    <style>
        #i1, #i2 {
            width: 100px;
            height: 30px;
        }
    </style>
</head>
<body>
<h3>计算器</h3>
<input type="text" id="i1" placeholder="请输入第一个值">
<input type="text" id="i2" placeholder="请输入第二个值">
<br>
<input type="button" value="加" onclick="f(1)">
<input type="button" value="减" onclick="f(2)">
<input type="button" value="乘" onclick="f(3)">
<input type="button" value="除" onclick="f(4)">
<div>结果:</div>
<script>
    let x = document.querySelector("#i1");
    let y = document.querySelector("#i2");
    let d = document.querySelector("div");

    function f(num) {
        if (isNaN(x.value) || isNaN(y.value)) {
            d.innerText = "输入错误";
            return;
        }
        switch (num) {
            case 1:
                d.innerText = (parseInt(x.value) + parseInt(y.value));
                break;
            case 2:
                d.innerText = (x.value - y.value);
                break;
            case 3:
                d.innerText = x.value * y.value;
                break;
            case 4:
                d.innerText = x.value / y.value;
                break;
        }
    }
</script>
</body>
</html>